package gui;

import business.EventManager;
import business.Notifier;
import business.UserManager;
import entity.EventScope;
import entity.LocationType;
import entity.WeatherType;
import entity.Event;
import entity.NotificationType;
import entity.User;
import java.io.Serializable;
import java.util.Date;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named(value = "eventBean")
@ViewScoped
public class EventBean implements Serializable {

    @EJB
    private EventManager eventManager;
    @EJB
    private UserManager userManager;
    @EJB
    private Notifier notifier;

    private int eventId;
    private Event event;
    private boolean modifiable = false;
    private boolean viewable = false;

    public EventBean() {
    }

    public void loadEvent() {
        if (eventId != 0) {
            event = eventManager.find(eventId);
        }
        if (event == null || event.getEventId() == null) {
            modifiable = true;
            viewable = true;
        }
        else if (event.getCreator() != null && event.getCreator().equals(userManager.getLoggedUser())) {
            modifiable = true;
            viewable = true;
        }
        else if (event.getParticipants().contains(userManager.getLoggedUser())) {
            viewable = true;
        }
    }

    public Event getEvent() {
        if (event == null) {
            event = new Event();
        }
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public int getEventId() {
        return eventId;
    }

    public void setEventId(int eventId) {
        this.eventId = eventId;
    }

    public WeatherType[] getThresholdChoices() {
        return WeatherType.values();
    }

    public LocationType[] getPossibleLocations() {
        return LocationType.values();
    }

    public EventScope[] getPossibleScopes() {
        return EventScope.values();
    }
    
    public Date getNow() {
        return new Date();
    }

    public boolean isModifiableByCurrentUser() {
        return modifiable;
    }

    public boolean isViewableByCurrentUser() {
        return viewable;
    }

    public String save() {
        User currentUser = userManager.getLoggedUser();
        if (eventManager.exists(eventId)) {
            eventManager.save(event);
        } else {
            event.setCreator(currentUser);
            currentUser.addCreatedEvent(event);
            currentUser.getCalendars().get(0).addEvent(event);
            eventManager.create(event);
            userManager.save(currentUser);
        }
        notifier.sendNotifications(event, NotificationType.MODIFICATION);
        return "eventDetails?faces-redirect=true&id=" + event.getEventId().toString();
    }

    public String delete() {
        eventManager.delete(event);
        return "home?faces-redirect=true";
    }
}
